﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.Controls;
using System.IO;

namespace ShopOnline
{
    

    public partial class ManageBanners : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ((AdminMaster)this.Master).SetPageTitle("Manage Banners");
                gridSlides.Rebind();
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "hideEditForm();", true);
            }
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            try
            {
                int PageBannersID = 0;
                if (hidID.Value == "-1")
                {
                    PageBannersID = 0;
                }
                else if (hidID.Value != "")
                {
                    PageBannersID = int.Parse(hidID.Value);
                }

                try
                {
                    ShopOnline.BLL.PageBanners slide = new ShopOnline.BLL.PageBanners();
                    if (PageBannersID > 0)
                        slide = ShopOnline.BLL.PageBanners.GetByID(PageBannersID);
                    else
                    {
                        slide.AddNew();
                        slide.Active = true;
                    }

                    string filename = string.Empty;
                    string[] arr = { "jpg", "png", "bmp", "gif" };

                    if (file.HasFile && !arr.Contains<string>(Path.GetExtension(file.FileName).ToLower().Replace(".", "")))
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'Invalid image!', 8000);", 1), true); 
                        return;
                    }
                    else if (file.HasFile)
                    {
                        filename = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName).ToLower();
                        //save image file
                        string path = Path.Combine(Server.MapPath("~"), Utilities.GetKeySetting("BannerArchive"));
                        DirectoryInfo dInfo = new DirectoryInfo(path);
                        if (!dInfo.Exists)
                            dInfo.Create();
                        file.SaveAs(Path.Combine(path, filename));
                        slide.s_FileName = filename;
                    }
                    else if(PageBannersID == 0)
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'Please select an image file!', 8000);", 1), true); 
                        return;
                    }
                    
                    slide.s_Title = txtTitle.Text.Trim();
                    slide.s_Description = txtDescription.Text.Trim();
                    slide.s_Page = ddlPage.SelectedValue;
                    slide.s_URL = txtURL.Text.Trim();
                    
                    slide.Save();
                    ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Save successfully!', 8000);", 1), true);
                    gridSlides.Rebind();

                }
                catch (Exception ex)
                {
                    ErrorLog.WriteToFile("ManagePageBanners.aspx.cs", "btnAddNew_OnClick", ex.StackTrace);
                }

                hidID.Value = "";
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "hideEditForm();", true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManagePageBanners.aspx.cs", "btnSave_OnClick", ex.StackTrace);
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showEditForm();", true);
            }
        }

        protected void dataPager_OnPageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            DataPagerCtrl pager = (DataPagerCtrl)sender;
            gridSlides.Rebind();
        }

        protected void gridSlides_OnNeedDataSource(object sender, EventArgs e)
        {
            ShopOnline.BLL.PageBanners slides = new BLL.PageBanners();
            slides.Search("", "[ID] desc", dataPager.CurrentPageIndex, dataPager.PageSize);
            gridSlides.DataSource = slides.DefaultView;
        }

        protected void chkActive_OnCheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            int id = int.Parse(chk.Attributes["ItemID"].ToString());
            ShopOnline.BLL.PageBanners slides = new BLL.PageBanners();
            slides.LoadByPrimaryKey(id);
            slides.Active = chk.Checked;
            slides.Save();

            gridSlides.Rebind();
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "hideEditForm();", true);
        }

        protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            int id = int.Parse(lnk.Attributes["ItemID"].ToString());
            hidID.Value = id.ToString();
            ShopOnline.BLL.PageBanners banner = new BLL.PageBanners();
            banner.LoadByPrimaryKey(id);

            txtTitle.Text = banner.s_Title;
            txtDescription.Text = banner.s_Description;
            txtURL.Text = banner.s_URL;
            ddlPage.SelectedValue = banner.Page;
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showEditForm(1);", true);
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            try
            {
                if (hidID.Value == "-1")
                {
                    ShopOnline.BLL.PageBanners slides = new BLL.PageBanners();
                    slides.LoadAll();
                    slides.DeleteAll();
                    slides.Save();
                }
                else if (hidID.Value != "")
                {
                    ShopOnline.BLL.PageBanners slides = new BLL.PageBanners();
                    slides.LoadByPrimaryKey(int.Parse(hidID.Value));
                    slides.DeleteAll();
                    slides.Save();
                }

                hidID.Value = "";
                gridSlides.Rebind();
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "hideEditForm();", true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManagePageBanners.aspx.cs", "btnDelete_OnClick", ex.StackTrace);
            }
        }
    }
}